﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common;
using Common.energyzmemDataSetTableAdapters;
using System.Data;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Data.Common;


namespace Provider
{
    public class Provider
    {
        energyzmemDataSet energyDataSet = new energyzmemDataSet();
        MySqlDataAdapter[] energyDataAdapters;
        DataTable[] energyTable;
        string[] TablesName;
        SourceType dataType;
        string targetFile;
        
        public Provider() {}

/// <summary>
/// 
/// </summary>
/// <param name="dataType"></param>
/// <param name="targetFile"></param>
/// <returns></returns>
        public energyzmemDataSet GetAllData(SourceType dataType, string targetFile) 
        {
            this.dataType = dataType;
            this.targetFile = targetFile;

            if (this.dataType == SourceType.XML)
            {
                this.energyDataSet.ReadXml(targetFile);
                return this.energyDataSet;
            }

            this.energyTable = new DataTable[]
            {
                this.energyDataSet.actsfuel,
                this.energyDataSet.actsmechan,
                this.energyDataSet.actspersonel,
                this.energyDataSet.actsresourcse,
                this.energyDataSet.actswork,
                this.energyDataSet.address,
                this.energyDataSet.aricles,
                this.energyDataSet.articlesmagazines,
                this.energyDataSet.authors,
                this.energyDataSet.borrows,
                this.energyDataSet.copies,
                this.energyDataSet.departments,
                this.energyDataSet.departmentsdocumentation,
                this.energyDataSet.dispatcher_name,
                this.energyDataSet.documentation,
                this.energyDataSet.employee,
                this.energyDataSet.employeedocumentation,
                this.energyDataSet.enterprise,
                this.energyDataSet.equipment,
                this.energyDataSet.itmes,
                this.energyDataSet.magazines,
                this.energyDataSet.record,
                this.energyDataSet.recordresourcse,
                this.energyDataSet.ref_accounts,
                this.energyDataSet.ref_documents,
                this.energyDataSet.ref_nomclature,
                this.energyDataSet.ref_record,
                this.energyDataSet.ref_types_of_work,
                this.energyDataSet.resources,
                this.energyDataSet.users,
                this.energyDataSet.work
            };
            this.TablesName = new string[] 
            { "actsfuel", "actsmechan", "actspersonel", "actsresourcse",  "actswork", "address", "aricles", "articlesmagazines", 
                "authors", "borrows", "copies", "departments", "departmentsdocumentation", "dispatcher_name",
                "documentation", "employee", "employeedocumentation", "enterprise", "equipment", "itmes", 
                "magazines", "record", "recordresourcse", "ref_accounts","ref_documents", "ref_nomclature",
                "ref_record", "ref_types_of_work", "resources", "users", "work"
            };

            this.energyDataAdapters = new MySqlDataAdapter[this.TablesName.Length];

            MySqlConnection con = new MySqlConnection(this.targetFile);

            for (int i = 0; i < this.TablesName.Length; i++)
            {
                this.energyDataAdapters[i] = new MySqlDataAdapter ("SELECT * FROM " + this.TablesName[i], con);

                MySqlCommandBuilder combld = new MySqlCommandBuilder(this.energyDataAdapters[i]);
            }
            try
            {
                for (int i = 0; i < this.energyDataAdapters.Length; i++)
                {
                    //DataTableMapping docum = energyDataAdapters[i].TableMappings.Add(
                    energyDataAdapters[i].Fill(energyTable[i]);
                }
            }
            catch
            {
                    this.energyDataSet = null;
            }
           
            return this.energyDataSet;
        }
/// <summary>
/// 
/// </summary>
        public void UpdataAllData()
        {
            if (this.dataType == SourceType.XML)
            {
                this.energyDataSet.WriteXml(this.targetFile);
                return;
            }

            for (int i = 0; i < this.energyDataAdapters.Length; i++)
                this.energyDataAdapters[i].Update(this.energyTable[i]);
        }
    }
}
 